package com.zjuh.qss.biz.ao.impl;

import wint.help.biz.result.Result;
import wint.help.biz.result.ResultSupport;
import wint.help.biz.result.ResultType;
import wint.lang.utils.StringUtil;

import com.zjuh.qss.biz.ao.UserAO;
import com.zjuh.qss.biz.dao.UserDAO;
import com.zjuh.qss.biz.domain.UserDO;
import com.zjuh.qss.biz.resultcodes.UserResultCodes;

public class UserAOImpl extends BaseAO implements UserAO {
	
	private UserDAO userDAO;
	
	public Result handleLogin(UserDO userDO) {
		Result result = new ResultSupport(false);
		UserDO userInDb = userDAO.queryByName(userDO.getName());
		if (userInDb == null) {
			result.setResultCode(UserResultCodes.INCORRECT_USERNAME, ResultType.CURRENT_TARGET);
			return result;
		}
		String pwdInDb = userInDb.getPassword();
		String pwdInput = encryptPassword(userDO.getPassword());
		if (!StringUtil.equals(pwdInDb, pwdInput)) {
			result.setResultCode(UserResultCodes.INCORRECT_PASSWORD, ResultType.CURRENT_TARGET);
			return result;
		}
		result.getModels().put("user", userInDb);
		result.setSuccess(true);
		return result;
	}

	private String encryptPassword(String pwd) {
		return  pwd;
	}
	
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}


}
